import * as borrowService from '../../services/borrow'
import { getJoberNum } from '../../services/shelfDown';
import { queryAssetType } from '../../services/assetType';
import { queryAssetBrand, queryAssetModelAll } from '../../services/assetModel';
import { qryDepts } from '../../services/department';
import { querAssetListByParam } from '../../services/equipment';
import { qryAllUser } from '../../services/user';
import { qryUserByDept } from '../../services/user';

export default {
    namespace: 'borrow',
    state: {
        borrowList: [],
        current: 1,
        currentSize: 10,
        total: 0,
        newKey: '',
        modalType: '',
        borrowId: '',
        assetTypeTree: [],
        assetBrandList: [],
        assetModelList:[],
        deptList: [],
        assetList: [],
        detailList: [],
        currentItem: {},
        userList: [],
        deptUserList:[],
        assetItem: {},
        editDetailList: [],
        borrowReturnDetailList: [],
        borrowReturnViewItem: {},
        viewItem: {},
        assetCurrent: 1,
        assetCurrentSize: 10,
        assetTotal: 0,
        qryAssetItem:{},
    },
    reducers: {
        updateState (state, { payload }) {
            return { ...state, ...payload}
        },
        qryListSuc(state, { payload }) {
            return { ...state, ...payload };
        },
        getJoberNumSuc(state, { payload }) {
            return { ...state, borrowId: payload };
        },
        queryAssetTypesSuc(state, { payload: assetTypeTree }) {
            return { ...state, assetTypeTree };
        },

        queryListByParamSuc(state, { payload }) {
            return { ...state, ...payload };
        },

        queryBrandListSuc(state, { payload: assetBrandList }) {
            return { ...state, assetBrandList };
        },

        queryDeptsSuc(state, { payload: deptList }) {
            return { ...state, deptList };
        },

        queryUserSuc(state, { payload: userList }) {
            return { ...state, userList };
        },
        qryUserByDeptSuc(state, { payload: deptUserList }) {
            return { ...state, deptUserList };
          },
        showModal(state, { payload }) {
            return { ...state, ...payload, newKey: new Date().getTime() + '', modalVisible: true }
        },

        hideModal(state, { payload }) {
            return { ...state, ...payload, modalVisible: false, detailList: [], newKey: new Date().getTime() + '' }
        },

        showViewModal(state, { payload }) {
            return { ...state, ...payload, newKey: new Date().getTime() + '', viewModalVisible: true }
        },
        hideViewModal(state) {
            return { ...state, newKey: new Date().getTime() + '', viewModalVisible: false, }
        },
        showSecondModal(state, { payload }) {
            return { ...state, ...payload, secondModalVisible: true }
        },

        hideSecondModal(state) {
            return { ...state, secondModalVisible: false }
        },

        changeDetailList(state, { payload }) {
            return { ...state, ...payload, secondModalVisible: false };
        },

        showAddBorrowDetailModal(state, { payload }) {
            return { ...state, ...payload, addBorrowDetailModalVisible: true }
        },
        hideAddBorrowDetailModal(state) {
            return { ...state, addBorrowDetailModalVisible: false }
        },

        updateDetaiListSuc(state, { payload }) {
            return { ...state, ...{ detailList: payload } }
        },

        showEditViewModal(state, { payload }) {
            return { ...state, ...payload, newKey: new Date().getTime() + '', editModalVisible: true }
        },
        hideEditViewModal(state, { payload }) {
            return { ...state, ...payload, newKey: new Date().getTime() + '', editModalVisible: false }
        },

        showBorrowReturnViewModal(state, { payload }) {
            return { ...state, ...payload, newKey: new Date().getTime() + '', borrowReturnModalVisible: true }
        },
        hideBorrowReturnViewModal(state, { payload }) {
            return { ...state, ...payload, newKey: new Date().getTime() + '', borrowReturnModalVisible: false }
        },

        showRemarkViewModal(state, { payload }) {
            return { ...state, ...payload, remarkModalVisible: true }
        },

        hideRemarkViewModal(state, { payload }) {
            return { ...state, ...payload, remarkModalVisible: false }
        },
        updateBorrowReturnDetailListSuc(state, { payload }) {
            return { ...state, ...{ borrowReturnDetailList: payload } }
        },
    },
    effects: {
        *queryUser({ payload }, { call, put }) {
            const { body } = yield call(qryAllUser);
            yield put({ type: 'queryUserSuc', payload: body.data });
        },
        *qryUserByDept({ payload }, { call, put }) {
            const { body } = yield call(qryUserByDept,payload);
            yield put({ type: 'qryUserByDeptSuc', payload: body.data });
          },
        *onViewItem({ payload }, { call, put }) {
            const { body } = yield call(borrowService.qryById, payload);
            yield put({
                type: 'showViewModal', payload: { viewItem: body.data, },
            });
        },

        *onAddViewItem({ payload }, { call, put }) {
            const { body } = yield call(getJoberNum, payload);
            yield put({ type: 'getJoberNumSuc', payload: body.data, });
            yield put({ type: 'showModal' });
            yield put({ type: 'queryAssetType' });
            yield put({ type: 'queryAssetBrand', payload: {}, });
            yield put({ type: 'queryDepts' });
        },
        *queryList({ payload }, { call, put }) {
            const { body } = yield call(borrowService.qryList, payload);
            yield put({ type: 'qryListSuc', payload: { borrowList: body.data, current: body.current, currentSize: body.size, total: body.iTotalRecords } });
        },

        *queryAssetType({ payload }, { call, put }) {
            const { body } = yield call(queryAssetType);
            yield put({ type: 'queryAssetTypesSuc', payload: body.data });
        },
        *queryAssetBrand({ payload }, { call, put }) {
            const { body } = yield call(queryAssetBrand, payload);
            yield put({ type: 'queryBrandListSuc', payload: body.data });
        },
        *queryAssetModel({ payload },{ call, put }) {
            const { body } = yield call(queryAssetModelAll,payload);
            yield put({ type: 'updateState', payload: {assetModelList:body.data} });
          },
        *queryDepts({ payload }, { call, put }) {
            const { body } = yield call(qryDepts);
            yield put({ type: 'queryDeptsSuc', payload: body.data });
        },

        *querAssetListByParam({ payload }, { call, put }) {
            const { body } = yield call(borrowService.querAssetListByParam, payload);
            yield put({ type: 'queryListByParamSuc', payload: { assetList: body.data, assetCurrent: body.current, assetCurrentSize: body.size, assetTotal: body.iTotalRecords } });
        },
        *queryAssetList({ payload }, { call, put }) {
            yield put({ type: 'querAssetListByParam', payload: {searchStatusType:5} });
            yield put({ type: 'showSecondModal', payload: { qryAssetItem: payload } });
          },

        *qryAssetById({ payload }, { call, put }) {
            // const { body } = yield call(borrowService.qryAssetById, payload);
            yield put({ type: 'showAddBorrowDetailModal', payload: { assetItem: payload } });
            yield put({ type: 'qryUserByDept',payload:{deptId:payload.useBranchId_}});
        },

        *updateAddDetaiList({ payload }, { call, put }) {
            yield put({ type: 'updateDetaiListSuc', payload });
            yield put({ type: 'hideAddBorrowDetailModal' });
        },

        *qryById({ payload }, { call, put }) {
            const { body } = yield call(borrowService.qryById, payload);
            yield put({
                type: 'showEditViewModal', payload: { editViewItem: body.data, editDetailList: body.data.borrowReturnDetail },
            });
            yield put({ type: 'queryAssetType' });
            yield put({ type: 'queryAssetBrand', payload: {}, });
            yield put({ type: 'queryDepts' });
        },
        *onReturnViewItem({ payload }, { call, put }) {
            const { body } = yield call(borrowService.qryBorrowListById, payload.id);
            yield put({
                type: 'showBorrowReturnViewModal',
                payload: {
                    borrowReturnViewItem: body.data,
                    borrowReturnDetailList: body.data.borrowReturnDetail,
                    modalType: payload.modalType
                },
            });
        },
        *onBorrowReturnViewItem({ payload }, { call, put }) {
            const { body } = yield call(borrowService.qryById, payload.id);
            yield put({
                type: 'showBorrowReturnViewModal',
                payload: {
                    borrowReturnViewItem: body.data,
                    borrowReturnDetailList: body.data.borrowReturnDetail,
                    modalType: payload.modalType
                },
            });
        },
        *updateBorrowReturnDetailList({ payload }, { call, put }) {
            yield put({ type: 'updateBorrowReturnDetailListSuc', payload });
            yield put({ type: 'hideRemarkViewModal' });
        },

        *create({ payload }, { call, put }) {
            const { body } = yield call(borrowService.create, payload);
            yield put({ type: 'hideModal' });
            yield put({ type: 'reload' });
        },
        *remove({ payload }, { call, put }) {
            const { body } = yield call(borrowService.remove, payload.id);
            yield put({ type: 'queryList', payload: payload.search });
        },

        *updateToBorrow({ payload }, { call, put }) {
            const { body } = yield call(borrowService.updateToBorrow, payload);
            yield put({ type: 'hideBorrowReturnViewModal' });
            yield put({ type: 'reload' });
        },

        *updateToReturn({ payload }, { call, put }) {
            const { body } = yield call(borrowService.updateToReturn, payload);
            yield put({ type: 'hideBorrowReturnViewModal' });
            yield put({ type: 'reload' });
        },

        *queryDetailById({ payload }, { call, put }) {
            // const { body } = yield call(borrowService.queryDetailById, payload);
            yield put({ type: 'showRemarkViewModal', payload: { currentItem: payload } });
        },
        *reload(action, { put }) {
            yield put({ type: 'queryList', payload: {} });
        },

    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathname === '/borrow/borrow') {
                    dispatch({ type: 'queryUser' });
                    dispatch({ type: 'queryList', payload: { 'status': '' } });
                }
            });
        },
    },
};
